//
//  DiscountHotVC.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "DiscountHotVC.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "mineLoginMailVC.h"
#import "DataBaseHandle.h"

@interface DiscountHotVC ()
{

}
@end

@implementation DiscountHotVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 60)];
    [webView setUserInteractionEnabled:YES];//是否支持交互
    //[webView setDelegate:self];
    //webView.delegate=self;
    [webView setOpaque:NO];//opaque是不透明的意思
    [webView setScalesPageToFit:YES];//自动缩放以适应屏幕
    [self.view addSubview:webView];
    NSString *string = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.8.5&track_deviceid=E3EBA485-00FA-421D-BACB-D791AF3400E2", self.discountId];

    //加载网页的方式
    //1.创建并加载远程网页
    NSURL *url = [NSURL URLWithString:string];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 15, WIDTH - 100, 60)];
    label.text = @"折扣详情";
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    //返回按钮
    UIButton *button = [self addButton:10 imageName:@"btn-fanhui64x64"];
    [button addTarget:self action:@selector(buttonAtcion) forControlEvents:UIControlEventTouchUpInside];
    //收藏按钮
    self.scButton = [self addButton:WIDTH - 90 imageName:@"btn_shoucang_kongxin"];
    [self.scButton addTarget:self action:@selector(scButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.isFavorite = NO;
    [self setFavoriteButton];
    
    // 分享按钮
    self.fxButton = [self addButton:WIDTH - 55
                          imageName:@"btn_shoucang_fenxiang"];
    [self.fxButton addTarget:self action:@selector(fxButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setFavoriteButton
{
    // creatDicountTableWithSQname
    DataBaseHandle *handle = [DataBaseHandle shareDataBaseHandle];
    UIImage *image = nil;
    if ([handle isSavedDiscount:self.discountId]) {
        image = [UIImage imageNamed:@"btn_shoucang_xuanzhong"];
        self.isFavorite = YES;
    } else {
        image = [UIImage imageNamed:@"btn_shoucang_kongxin"];
        self.isFavorite = NO;
    }
    [self.scButton setImage:image forState:UIControlStateNormal];
}

//
- (UIButton *)addButton:(NSInteger )row imageName:(NSString *)imageName
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(row, 20, 40, 40);
    button.backgroundColor = [UIColor clearColor];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self.view addSubview:button];
    return button;
}

//返回按钮
- (void)buttonAtcion
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

//收藏按钮
- (void)scButtonAction:(UIButton *)button
{
    
    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defa objectForKey:@"userName"];
    if (userName == nil) {
        mineLoginMailVC *loginMailVC = [[mineLoginMailVC alloc] init];
        [self presentViewController:loginMailVC animated:YES completion:^{
            
        }];
    }else
    {
        if (self.isFavorite) {
            DataBaseHandle *handle = [DataBaseHandle shareDataBaseHandle];
            if ([handle  deleteDiscountWithID:self.discountId userName:userName]) {
                [self showAlert:@"已成功取消收藏"];
                [button setImage:[UIImage imageNamed:@"btn_shoucang_kongxin"] forState:UIControlStateNormal];
                  self.isFavorite = NO;
            }
//            self.myFaveriteIDBlock(self.isFavorite, self.discountId);
        } else {
            [button setImage:[UIImage imageNamed:@"btn_shoucang_xuanzhong"] forState:UIControlStateNormal];
            [self showAlert:@"收藏成功!"];
            self.isFavorite = YES;
            self.myFaveriteIDBlock(self.isFavorite, self.discountId);
        }
    }
}

//分享按钮
- (void)fxButtonAction:(UIButton *)button
{
    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defa objectForKey:@"userName"];
    if (userName == nil) {
        mineLoginMailVC *loginMailVC = [[mineLoginMailVC alloc] init];
        [self presentViewController:loginMailVC animated:YES completion:^{
            
        }];
    }else
    {

        /**
         根据平台名，返回平台对象
         
         @param platformName sns平台名
         
         @return UMSocialSnsPlatform 平台对象
         */
        
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        /**
         Sns平台类，用`platformName`作为标识，指定显示名称、显示的图片，点击之后的响应。
         */
        snsPlatform.displayName = @"新浪";
        snsPlatform.loginName = @"网易";
        
        UMSocialSnsPlatform *snsPlatform2 = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
        snsPlatform2.displayName = @"腾讯微博";
        
        //分享第二步
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"558936d367e58eb81b001298"
                                          shareText:@"你要分享的文字"
                                         shareImage:[UIImage imageNamed:@"Icon-29.png"]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                           delegate:self];
   
 }
}


- (void)timerFireMethod:(NSTimer*)theTimer  //弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}


- (void)showAlert:(NSString *) _message{   //时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.5f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
